import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2022/7/19 14:18
 */
public class Join_1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
    
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.getConfig().getConfiguration().setString("pipeline.name", "abc");
        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(10));
        
        tEnv.executeSql("create table a(id string, name string)with(" +
                            "'connector' = 'kafka', " +
                            "'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "'properties.group.id' = 'atguigu111', " +
                            "'topic' = 'a', " +
                            "'format' = 'csv'" +
                            ")");
    
        tEnv.executeSql("create table b(id string, age int)with(" +
                            "'connector' = 'kafka', " +
                            "'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "'properties.group.id' = 'atguigu', " +
                            "'topic' = 'b', " +
                            "'format' = 'csv' " +
                            ")");
        // 内连接
        /*tEnv.sqlQuery("select " +
                          "a.id, " +
                          "name, " +
                          "age " +
                          "from a " +
                          "join b on a.id=b.id")
            .execute()
            .print();*/
        // 左连接
        Table result = tEnv.sqlQuery("select " +
                                        "a.id, " +
                                        "name, " +
                                        "age " +
                                        "from a " +
                                        "left join b on a.id=b.id");
        tEnv.executeSql("create table ab(id string, name string, age int, primary key(id) not enforced) with(" +
                            "'connector' = 'upsert-kafka', " +
                            "'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "'topic' = 'ab', " +
                            "'key.format' = 'json', " +
                            "'value.format' = 'json' " +
                            ")");
        
        result.executeInsert("ab");
    
    
        try {
            env.execute("abc");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
